package com.summary.richtext.utils.richExcelUtil;

import com.summary.richtext.utils.enums.RichFontStyle;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFColor;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class PoiStyleUtil {

    private static String s = "0123456789ABCDEF";

    /**
     * 获取字体大小（px）
     * @param size
     * @return
     */
    public static int getFontSize(String size) {
        HashMap<Integer, String> sizeMap = RichFontStyle.getSizeMap();
        for (Integer item : sizeMap.keySet()) {
            String s = sizeMap.get(item);
            if (StringUtils.equals(size, s)) {
                return item;
            }
        }
        //默认字体大小下索引（10）
        return 10;
    }

    /**
     * 获取颜色索引（用于excel导出时识别rgb颜色索引）
     * 注：XSSF 的颜色索引依据亦可来源于 HSSF，相关详情：DefaultIndexedColorMap.getDefaultRGB 调用了 HSSFColor.getIndexHash()，与getMutableIndexHash同源;
     * 因此此处不考虑区分XSSF、HSSF
     * @param rgb
     * @return
     */
    public static int getColor(String rgb) {
        Map<Integer, HSSFColor> mutableIndexHash = HSSFColor.getMutableIndexHash();
        for (Integer item : mutableIndexHash.keySet()) {
            HSSFColor hssfColor = mutableIndexHash.get(item);
            String hssfargbHex = getHSSFARGBHex(hssfColor);
            if (StringUtils.equals(hssfargbHex, rgb)) {
                return item;
            }
        }
        //默认黑色字体索引（8）
        return 8;
    }

    /**
     * 字体颜色转化为16进制XSSF
     */
    public static String getXSSFARGBHex(XSSFColor xssfColor) {
        if (xssfColor != null) {
            String argbHex = xssfColor.getARGBHex();
            if (argbHex!=null) {
                String substring = argbHex.substring(2);
                substring = "#" + substring;
                return substring;
            }
        }
        return "#000000";
    }

    /**
     * 字体颜色转化为16进制HSSF
     */
    public static String getHSSFARGBHex(HSSFColor hssfColor) {
        if (hssfColor != null) {
            short[] triplet = hssfColor.getTriplet();
            String ARGBHex = ConvertRGBToHex(triplet[0], triplet[1], triplet[2]);
            return ARGBHex;
        }
        return "#000000";
    }

    /**
     * RGB转换成十六进制
     *
     * @param r
     * @param g
     * @param b
     * @return
     */
    public static String ConvertRGBToHex(int r, int g, int b) {
        String hex = "";
        if (r >= 0 && r < 256 && g >= 0 && g < 256 && b >= 0 && b < 256) {
            int x, y, z;
            x = r % 16;
            r = (r - x) / 16;
            y = g % 16;
            g = (g - y) / 16;
            z = b % 16;
            b = (b - z) / 16;
            hex = "#" + s.substring(r, r + 1) + s.substring(x, x + 1) + s.substring(g, g + 1) + s.substring(y, y + 1) + s.substring(b, b + 1) + s.substring(z, z + 1);
        }
        return hex;
    }

    /**
     * 十六进制转换成RGB
     *
     * @param hex
     * @return
     */
    public static String ConvertHexToRGB(String hex) {
        String rgb = "";
        String regex = "^[0-9A-F]{3}|[0-9A-F]{6}$";
        if (hex != null) {
            hex = hex.toUpperCase();
            if (hex.substring(0, 1).equals("#")) {
                hex = hex.substring(1);
            }
            if (Pattern.compile(regex).matcher(hex).matches()) {
                String a, c, d;
                String[] str = new String[3];
                for (int i = 0; i < 3; i++) {
                    a = hex.length() == 6 ? hex.substring(i * 2, i * 2 + 2) : hex.substring(i, i + 1) + hex.substring(i, i + 1);
                    c = a.substring(0, 1);
                    d = a.substring(1, 2);
                    str[i] = String.valueOf(s.indexOf(c) * 16 + s.indexOf(d));
                }
                rgb = str[0] + "," + str[1] + "," + str[2];
            }
        }
        return rgb;
    }

}
